#include<iostream>
#include<vector>
#include<string.h>
#include<stdio.h>
#include<assert.h>
using namespace std;

namespace v
{
	template<class t>
	class vector
	{
	public:
		typedef t* iterator;
		vector()
			:_start(nullptr)
			, _finsh(nullptr)
			, _end(nullptr)
		{

		}
		~vector()
		{
			delete[] _start;
			_start = _finsh = _end = nullptr;
		}
		void resize(size_t d1,t d2 = '\0')
		{
			assert(d1 < capacity() && d1 > 0);
			if (d1 < size())
			{
				while (_finsh != _start + d1)
				{
					--_finsh;
				}
			}
			else
			{
				if (size() == capacity())
				{
					reserve(capacity() * 2);
				}
				auto tmp = _finsh;
				while (size() != d1)
				{
					*_finsh = d2;
					++_finsh;
				}
			}
		}
		//iterator erase(iterator it);
		//iterator erase(iterator first, iterator last);

		iterator erase(iterator it)
		{
			assert(it >= _start && it <= _finsh);
			auto end = it;
			while (end <= _finsh)
			{
				*end = *(end + 1);
				end++;
			}
			--_finsh;
			return it;
		}
		iterator erase(iterator first, iterator last)
		{
			while (first != last)
			{
				erase(first);
				++first;
			}
			return last + 1;
		}

		void reserve(size_t d1)
		{
			iterator tmp = new t[d1];
			memcpy(tmp, _start, sizeof(t) * size());
			delete[] _start;
			_finsh = tmp + size();
			_start = tmp;
			_end = _start + d1;
		}
		void push_back(t d1)
		{
			size_t ca;

			if (capacity() == size())
			{
				if (capacity() == 0)
				{
					ca = 4;
					reserve(ca);
				}
				else
				{
					ca = capacity() * 4;
					reserve(ca);
				}
				//size_t ca = capacity() == 0 ? 4 : ca * 2;
				//if (_start)
				//{
				//	/*memcpy(tmp, _start, sizeof(t) * size());
				//	delete[] _start;*/
				//	reserve(ca);
				//}

			}
			*_finsh = d1;
			++_finsh;
		}
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finsh;
		}
		size_t capacity()
		{
			return _end - _start;
		}
		size_t size()
		{
			return _finsh - _start;
		}
		t& operator[](int i)
		{
			return _start[i];
		}
	private:
		iterator _start;
		iterator _finsh;
		iterator _end;
	};

}

